angular.module('tmsApp', ['ngRoute'])
.config(['$routeProvider', ($routeProvider) ->
  $routeProvider.when('/login', {
    templateUrl: '/app/login/index.html'
    controller: 'LoginCtrl'
  }).when('/register', {
    templateUrl: '/app/register/index.html'
    controller: 'RegisterCtrl'
  }).when('/', {
    templateUrl: '/app/index/index.html'
    controller: 'IndexCtrl'
  })
])
.run(['$location', '$http', ($location, $http)->
  token = localStorage.getItem('x-token')
  if token
    $http.defaults.headers.common['x-token'] = token
    $http.post("#{Tms.apiAddress}/api/user/autologin", {token: token})
    .then((res)->
      if res.data is true
#登陆后设置token
        $http.defaults.headers.common['x-token'] = token
        #并保存到localStorage
        localStorage.setItem('x-token', token)
        $location.path('/').replace()
      else
        $location.path('/login').replace()
    , ->
      $location.path('/login').replace()
    )
  else
    $location.path('/login').replace()
])